package com.wangchao.administrator.zenmechi.ui.searchactivity;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.wangchao.administrator.zenmechi.bean.caipudetailbean.CaipuDetail;
import com.wangchao.administrator.zenmechi.bean.caipudetailbean.CaipuDetailDataBean;
import com.wangchao.administrator.zenmechi.bean.merchantbean.MerchantBean;
import com.wangchao.administrator.zenmechi.bean.merchantbean.MerchantDataBean;
import com.wangchao.administrator.zenmechi.bean.productbean.ProductBean;
import com.wangchao.administrator.zenmechi.bean.productbean.ProductDataBean;
import com.wangchao.administrator.zenmechi.config.UrlConfig;
import com.wangchao.administrator.zenmechi.db.SearchHistory;
import com.wangchao.administrator.zenmechi.db.SearchHistoryDbHelper;
import com.wangchao.administrator.zenmechi.ui.fragment.caipuitemfragment.MyAdapterForCaipuList;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Subscriber;

/**
 * Created by Administrator on 2016/10/18.
 */
public class SearchActivityPresenter implements SearchActivityContract.Presenter{

    private SearchActivityContract.View view;
    private SearchActivityContract.Model model;
    private String name,searchType;
    private Map<String,String> map = new HashMap<>();
    private List<CaipuDetailDataBean> beans;
    private MyAdapterForCaipuList adapter;
    private List<ProductDataBean> productBeens;
    private MyAdapterForProductSearch adapterForProductSearch;
    private List<MerchantDataBean> merchantBeens;
    private MyAdapterForMerchant adapterForMerchant;
    private List<SearchHistory> historyList = new ArrayList<>();
    private MyAdapterForHistory adapterForHistory;
    private SearchHistoryDbHelper helper;
    private Dao<SearchHistory,Long> HistoryDao;

    public SearchActivityPresenter(SearchActivityContract.View view) {
        this.view = view;
        this.model = new SearchActivityModel();
    }

    public void clearHistory(Context context){
        int delete = 0;
        try {
            helper = SearchHistoryDbHelper.getInstance(context);
            HistoryDao = helper.getHistoryDao();
            delete = HistoryDao.delete(historyList);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (delete>0){
            Log.i("tag","-----删除成功-----");
        }
        searchHistory(context);
    }

    @Override
    public void searchHistory(final Context context) {
        model.searchHistory(new Subscriber<List<SearchHistory>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(List<SearchHistory> searchHistories) {
                historyList = searchHistories;
                //Log.i("tag","----------"+historyList.size());
                adapterForHistory = new MyAdapterForHistory(context,historyList);
                view.initSearchCaipu(adapterForHistory);
            }
        },context);
    }

    private void createHistory(Context context) {
        helper = SearchHistoryDbHelper.getInstance(context);
        HistoryDao = helper.getHistoryDao();
        name = view.getSearchName();
        if (!TextUtils.isEmpty(name)) {
            SearchHistory history = new SearchHistory(name);
            try {
                HistoryDao.createIfNotExists(history);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void search(Context context) {
        searchType = view.getSearchType();
        if (searchType.equals("菜谱")){
            searchForCaipu(context);
        }
        if (searchType.equals("产品")){
            searchForProduct(context);
        }
        if (searchType.equals("商铺")){
            searchMerchant(context);
        }
    }

    public void initMap(){
        name = view.getSearchName();
        if (!TextUtils.isEmpty(name)){
            map.put(UrlConfig.Params.MARKET_ID,UrlConfig.DefaultVaule.MARKET_ID+"");
            map.put(UrlConfig.Params.NAME,name);
            map.put(UrlConfig.Params.PAGE,UrlConfig.DefaultVaule.PAGE_NUM+"");
            map.put(UrlConfig.Params.PAGE_SIZE,UrlConfig.DefaultVaule.PAGE_SIZE+"");
        }
    }

    @Override
    public void searchForCaipu(final Context context) {
        initMap();
        createHistory(context);
        model.searchForCaipu(map, new Subscriber<CaipuDetail>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Log.i("tag","----------"+e.toString());
            }

            @Override
            public void onNext(CaipuDetail caipuDetail) {
                beans = caipuDetail.getResult().getData();
                if (beans!=null&&beans.size()!=0){
                    //Log.i("tag","----------"+beans.size());
                    adapter = new MyAdapterForCaipuList(context,beans);
                    view.initSearchCaipu(adapter);
                }else {
                    view.hideNone();
                }
            }
        });
    }

    @Override
    public void searchForProduct(final Context context) {
        initMap();
        createHistory(context);
        model.searchForProduct(map, new Subscriber<ProductBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Log.i("tag","----------"+e.toString());
            }

            @Override
            public void onNext(ProductBean productBean) {
                productBeens = productBean.getResult().getData();
                if (productBeens!=null&&productBeens.size()!=0){
                    view.hideNone();
                    //Log.i("tag","----------"+productBeens.size());
                    adapterForProductSearch = new MyAdapterForProductSearch(context,productBeens);
                    view.initSearchCaipu(adapterForProductSearch);
                }else {
                    view.showNone();
                }
            }
        });
    }

    @Override
    public void searchMerchant(final Context context) {
        initMap();
        createHistory(context);
        model.searchMerchant(map, new Subscriber<MerchantBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(MerchantBean merchantBean) {
                merchantBeens = merchantBean.getResult().getData();
                //Log.i("tag","----------"+merchantBeens.size());
                if (merchantBeens!=null&&merchantBeens.size()!=0){
                    view.hideNone();
                    adapterForMerchant = new MyAdapterForMerchant(context,merchantBeens);
                    view.initSearchCaipu(adapterForMerchant);
                }else {
                    view.showNone();
                }
            }
        });
    }
}
